package com.heer.gymnasium.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.model.BookOrderBean;

import java.util.List;

/**
 * Created by heer on 17/4/7.
 */

public class SiteReservedPriceAdapter extends RecyclerView.Adapter<SiteReservedPriceAdapter.MyViewHolder> {

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private List<BookOrderBean> mDatas;
    private OnItemClickLitener itemClickLitener;

    public SiteReservedPriceAdapter(List<BookOrderBean> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                parent.getContext()).inflate(R.layout.item_site_reserved_price, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        BookOrderBean item = mDatas.get(position);
        holder.tv.setText(item.getPrice());
        if (position % mDatas.get(0).getNum() == 0) {
            holder.tv.setBackgroundColor(Color.WHITE);
            holder.tv.setTextColor(holder.itemView.getResources().getColor(R.color.colorTextNormal));
            return;
        }
        if (TextUtils.equals("2", item.getStatus()))
            holder.tv.setBackgroundResource(R.drawable.shape_orange);
        if (TextUtils.equals("1", item.getStatus()))
            holder.tv.setBackgroundResource(R.drawable.shape_primary);
        if (TextUtils.equals("0", item.getStatus()))
            holder.tv.setBackgroundResource(R.drawable.shape_gray);

        holder.tv.setTextColor(Color.WHITE);

        // 如果设置了回调，则设置点击事件
        if (itemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    itemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    itemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public BookOrderBean getItem(int position) {
        return mDatas.get(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv_price);
        }
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.itemClickLitener = mOnItemClickLitener;
    }
}